package com.ctshk.common.web.utils;

import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.ctshk.common.utils.Assist;

/**
 * Servlet工具类
 * 
 * @author Qiaoxin.Hong
 *
 */
public class ServletUtils {

	/**
	 * 取得ServletRequestAttributes
	 * @return
	 */
	public static ServletRequestAttributes getServletRequestAttributes() {
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if (requestAttributes == null) return null;
		if (requestAttributes instanceof ServletRequestAttributes) {
			return (ServletRequestAttributes) requestAttributes;
		}
		return null;
	}
	
	/**
	 * 取得HttpServletRequest
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		return Assist.ifNotNullFn(getServletRequestAttributes(), ServletRequestAttributes::getRequest);
	}
	
	/**
	 * 取得HttpServletResponse
	 * @return
	 */
	public static HttpServletResponse getResponse() {
		return Assist.ifNotNullFn(getServletRequestAttributes(), ServletRequestAttributes::getResponse);
	}
	
	/**
	 * 取得下载用的HttpServletResponse
	 * @param response
	 * @param fileName
	 * @return
	 */
	public static HttpServletResponse getDownloadResponse(String fileName) {
		return getDownloadResponse(getResponse(), fileName);
	}
	
	/**
	 * 取得下载用的HttpServletResponse
	 * @param response
	 * @param fileName
	 * @return
	 */
	public static HttpServletResponse getDownloadResponse(HttpServletResponse response, String fileName) {
		return getDownloadResponse(response, fileName, "application/octet-stream");
	}
	
	/**
	 * 取得下载用的HttpServletResponse
	 * @param response
	 * @param fileName
	 * @return
	 */
	public static HttpServletResponse getDownloadResponse(HttpServletResponse response, String fileName, String contentType) {
		Assist.notNull(response, "response cannot be null");
		Assist.notBlank(fileName, "fileName cannot be blank");
		Assist.notBlank(contentType, "contentType cannot be blank");
		
		try {
			//文件名
			String downloadFileName = fileName;
			
//			response.setCharacterEncoding("UTF-8");
			response.setContentType(contentType);
//			response.setHeader("Access-Control-Expose-Headers", "Content-Disposition,Access-Token,Uid");
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(downloadFileName, "UTF-8"));
			
			return response;
		} catch (Exception e) {
			throw new RuntimeException("get download response error", e);
		}
	}
	
	/**
	 * 提取HttpServletResponse下载用的OutputStream，
	 * @param response
	 * @param fileName
	 * @return
	 */
	public static OutputStream getDownloadOutputStream(String fileName) {
		return getDownloadOutputStream(getResponse(), fileName);
	}
	
	/**
	 * 提取HttpServletResponse下载用的OutputStream，
	 * @param response
	 * @param fileName
	 * @return
	 */
	public static OutputStream getDownloadOutputStream(HttpServletResponse response, String fileName) {
		return getDownloadOutputStream(response, fileName, "application/octet-stream");
	}
	
	/**
	 * 提取HttpServletResponse下载用的OutputStream
	 * @param response
	 * @param fileName
	 * @return
	 */
	public static OutputStream getDownloadOutputStream(String fileName, String contentType) {
		return getDownloadOutputStream(getResponse(), fileName, contentType);
	}
	
	/**
	 * 提取HttpServletResponse下载用的OutputStream
	 * @param response
	 * @param fileName
	 * @return
	 */
	public static OutputStream getDownloadOutputStream(HttpServletResponse response, String fileName, String contentType) {
		try {
			getDownloadResponse(response, fileName, contentType);
			return response.getOutputStream();
		} catch (Exception e) {
			throw new RuntimeException("get download OutputStream error", e);
		}
	}
}
